<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>客户聊天</title>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/bootcdn/BootCDN/ajax/libs/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="<%=path%>/css/public.css" />
<link rel="stylesheet" type="text/css" href="<%=path%>/css/custom.css" />
<link rel="stylesheet" type="text/css" href="<%=path%>/css/set.css" />
<style type="text/css">
.m_xhistory{
	position:static;
	width: 100%;
}
</style>
</head>
<body>
    <div class="m_xhistory">
        <div class="m_xrecord">
            <div class="m_xpersona">
                <div class="name">
                    <i><img src="<%=path%>/images/000head_pic.png" /></i>
                    <span></span>
                    <a href="javascript:;" class="edit" id="customerEdit"></a>
                </div>
                <div class="contact">
                    <div class="rows"><img src="<%=path%>/images/disp_1.png" /><span id="spanQq"></span></div>
                    <div class="rows"><img src="<%=path%>/images/disp_2.png" /><span id="spanWeibo"></span></div>
                    <div class="rows"><img src="<%=path%>/images/disp_3.png" /><span id="spanWeixin"></span></div>
                    <div class="rows"><img src="<%=path%>/images/disp_4.png" /><span id="spanPhone"></span></div>
                    <div class="rows last"><img src="<%=path%>/images/disp_5.png" /><span id="spanEmail"></span></div>
                    <div class="tans">
                    </div>
                    <div class="cons">累计消费:<span id="spanTotalCost">无</span></div>
                    <div class="cons">最近消费:<p><em id="emMaxCost">无</em><span id="spanMaxDate">无</span></p></div>
                    <div class="cons">第一次消费:<p><em id="emMinCost">无</em><span id="spanMinDate">无</span></p></div>
                </div>
            </div>
            <div class="m_xbosom">
                <div class="m_xtalk">
                	<div class="conve">
                		<span class="time">2017-08-10 09:47:50</span>
                		<div class="main"><i><img src="<%=basePath%>/images/000head_pic.png"></i>
                			<p><strong><b>张三</b></strong>
                				<em>微博内容</em>
               					<a id="status_reposts_count">转发：0</a>&nbsp;&nbsp;
               					<a id="status_comments_count">评论：0</a>&nbsp;&nbsp;
               					<a id="status_attitudes_count">赞同：0</a>
                			</p>
                		</div>
                	</div>
                    <div class="service">
                    	<span class="time">2017-08-10 11:01:02</span>
                    	<div class="busin"><i><img src="<%=basePath%>/images/demo_7.png"></i><div class="hails"><b><img src="<%=basePath%>/images/san.png"></b><em>来自sun</em><abbr>abcdefg</abbr></div></div>
                    </div>
                </div>
                <div class="select J_xselect">
                    <a href="javascript:;">微博</a>
                    <a href="javascript:;">短信</a>
                    <a href="javascript:;">邮箱</a>
                </div>
                <textarea class="import">尊敬的顾客您好！</textarea>
                <a href="javascript:;" class="btns btns_send">发 送</a>
            </div>
            <div class="m_xmoney">
                <div class="more"><a href="javascript:;" class="J_xseed">更多</a>推荐活动</div>
                <div class="kelvin J_xkelvin">
                </div>
<!--                 <div class="area"><textarea id="area" placeholder="备注，enter键提交" maxlength="30" onblur="checkLength(this)"></textarea></div> -->
            </div>
        </div>
    </div>
	<div class="m_xpopups J_xactivity">
        <div class="m_xpropell">
            <div class="wire"></div>
            <div class="title"><i class="J_xsutup"><img src="<%=path%>/images/close.png" /></i>选择推送活动</div>
            <div class="m_xswung">
                <div class="caption">
                    <span>活动概览</span>
                    <!-- 
                    <span>活动标签</span>
                     -->
                    <span>统计值</span>
                    <span>状态</span>
                </div>
                <ul class="flexibe" id="activity_list">
                </ul>
                <div class="m_xpages">
					<div class="mian">
						<span>显示行数</span> <select><option>5</option>
							<option>10</option>
							<option>20</option></select> <span>共0条</span> <span>1/0页</span> <a
							href="javascript:;" class="prev">上一页</a> <a href="javascript:;"
							class="next">下一页</a> <span>前往</span> <input type="text" value="1"
							class="text"> <span>页</span>
					</div>
				</div>
                <div class="m_xbutton m_xfinish">
                    <a href="javascript:;" class="white">取消</a>
                    <a href="javascript:;" onclick="childFinish()">完成</a>
                </div>
            </div>
        </div>
     </div>
     
     <div class="m_xpopups j_customerEdit">
		<div class="m_xconsum show" style="height: 600px;overflow-y:auto;">
			<div class="wire"></div>
			<div class="title">
				<i class="J_xclose"><img src="<%=path%>/images/close.png" /></i><span>修改客户</span>
			</div>
			<div class="m_xline" id="nameadd">
				<span>姓名</span>
				<div class="right">
					<input type="text" class="text" placeholder="请输入姓名" id="namea"
						name="name" />
				</div>
			</div>
			<div class="m_xline" id="phoneadd">
				<span>手机号</span>
				<div class="right">
					<input type="text" class="text" placeholder="请输入手机号" id="phonea"
						name="phone" />
				</div>
			</div>
			<div class="m_xline" id="emailadd">
				<span>邮箱</span>
				<div class="right">
					<input type="text" class="text" placeholder="请输入邮箱" id="emaila"
						name="email" />
				</div>
			</div>
			<div class="m_xline" id="companyadd">
				<span>公司</span>
				<div class="right">
					<input type="text" class="text" placeholder="请输入公司名称" id="companya"
						name="company" />
				</div>
			</div>
			<div class="m_xline" id="jobadd">
				<span>职位</span>
				<div class="right">
					<input type="text" class="text" placeholder="请输入职位" id="joba"
						name="joba" />
				</div>
			</div>
			<div class="m_xline" id="remarksadd">
				<span>备注</span>
				<div class="right">
					<input type="text" class="text" placeholder="请输入备注" id="remarksa"
						name="remarks" />
				</div>
			</div>
			<div class="m_xline" id="genderadd">
				<span>性别</span>
				<div class="right">
					<input type="text" class="text" placeholder="请输入性别" id="gendera"
						name="remarks" />
				</div>
			</div>
			<div class="m_xline" id="birthdayadd">
				<span>生日</span>
				<div class="right">
					<input type="text" class="text" placeholder="请输入生日" id="birthdaya"
						name="birthdaya" />
				</div>
			</div>
			<div class="m_xline" id="weiboadd">
				<span>微博</span>
				<div class="right">
					<input type="text" class="text" placeholder="请输入微博" id="weiboa"
						name="weiboa" />
				</div>
			</div>
			<div class="m_xline" id="qqadd">
				<span>QQ</span>
				<div class="right">
					<input type="text" class="text" placeholder="请输入QQ" id="qqa"
						name="qqa" />
				</div>
			</div>
			<div class="m_xline" id="wechatadd">
				<span>微信</span>
				<div class="right">
					<input type="text" class="text" placeholder="请输入微信" id="wechata"
						name="wechata" />
				</div>
			</div>
			<div class="m_xline" id="addressadd">
				<span>地址</span>
				<div class="right">
					<input type="text" class="text" placeholder="请输入地址" id="addressa"
						name="addressa" />
				</div>
			</div>
			<div class="m_xline" id="cityadd">
				<span>城市</span>
				<div class="right">
					<input type="text" class="text" placeholder="请输入城市" id="citya"
						name="citya" />
				</div>
			</div>
			<div class="m_xline" id="faxadd">
				<span>传真</span>
				<div class="right">
					<input type="text" class="text" placeholder="请输入传真" id="faxa"
						name="faxa" />
				</div>
			</div>
			<div class="m_xline" id="tagadd">
				<span>标签</span>
				<div class="right">
					<input type="text" class="text" placeholder="请输入标签" id="taga"
						name="taga" />
				</div>
			</div>
			<div class="m_xline">
				<span></span>
				<div class="right">
					<a href="javascript:;" class="btns" onclick="saveOrUpdate()">保 存</a>
				</div>
			</div>
		</div>
	</div>
	
	<!-- 弹框 -->
	<div class="s_popup_box j_popup_box">
        <div class="s_popup_detailed show">
            <h4><img src="<%=path%>/images/icon_hint.png" /><span>操作失败</span></h4>
            <p><a href="javascript:;" onclick="javascript:$(this).parents('.j_popup_box').hide();">确定</a></p>
        </div>
    </div>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script type="text/javascript" src="<%=path%>/js/custom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script type="text/javascript" src="<%=path%>/js/custom_activity_list.js"></script>
<script type="text/javascript" src="<%=path%>/js/lengthlimit.js"></script>
<script>
var path = '${pageContext.request.contextPath}';
var userName = decodeURI($.cookie('user_name'));
var msg = [];
    $(function(){   
//       $('.k_select').on('click',function(){
//         $(this).toggleClass('selectState');
//         if(!$(this).hasClass('selectState')){
//           $('.k_select').removeClass('selectState');
<%--           $(this).find('img').attr('src','<%=path%>/images/dropDown.png'); --%>
//         }else{
<%--           $(this).find('img').attr('src','<%=path%>/images/dropUp.png'); --%>
//         }
//       });
      $('#area').on('keydown',function(e){
        if(e.keyCode==13){
            e.preventDefault();
            var appendText = $.trim($('#area').val());
            if(appendText!=""){
            
            	$.post("<%=path%>/api/customer_action/addChatRemark",{customerId:customerId,actionId:actionId,clueWeiboId:clueWeiboId,content:appendText,writeOffId:writeOffId},function(data){
            		if(data.msg == "success"){
		                var appendHTML = '<div class="businTop"><span class="time timeBox">'+new Date().format("yyyy-MM-dd hh:mm")+'</span><div class="busin businBox"><div class="hails"><dfn>'+appendText+'</dfn></div><div class="edit"><img src="<%=path%>/images/xicon_4.png" class="k_close_icon"></div></div></div>';
		                $('.m_xtalk').append(appendHTML);
		              	//定位滚动轴
		            	$(".m_xtalk").animate({scrollTop:$('.m_xtalk')[0].scrollHeight},2000);
            		}else{
            			$(".j_popup_box span").text("操作失败");
                    	$(".j_popup_box").show();
            		}
            	},"json");
            	
            }
            $("#area").val("");
        }
         //$('.m_xtalk').scrollTop( $('.m_xtalk')[0].scrollHeight )
      })
      var data_index = 0;
       msg = [];
	      $(document).off('click','.J_xkelvin .cash').on('click','.J_xkelvin .cash',function(){
	      	$(this).toggleClass('on');
	        if($(this).hasClass('on')){
	            data_index++;
	            $(this).attr('data-index',data_index);
	            msg.push($(this).find('span').text()+"("+$(this).attr('short_url')+")");
	        }else{
	            var nowIndex = $(this).attr('data-index');
	            msg[nowIndex-1] = "";
	            $(this).removeAttr('data-index',data_index);
	        }
	        $('.import').val("");
	        var text = "";
	        for( var i = 0;i<msg.length;i++){
	            if(msg[i]!=""){
	               text+=msg[i]+"\r"; 
	            }
	        }
	        $('.import').val(text);
	      })
      $('body').on('click','.k_close_icon',function(){
        	//$(this).parents('.businTop').remove();
      		var chatRemarkId = $(this).attr("chatRemarkId");
      		var temp = $(this);
      		
      		$.post("<%=path%>/api/customer_action/delChatRemark",{id:chatRemarkId},function(data){
      			if(data.msg == "success"){
      				temp.parents('.businTop').remove();
      			}else{
      				$(".j_popup_box span").text("操作失败");
                	$(".j_popup_box").show();
        		}
      		},"json");
      		
      		
      })

      $('.J_xselect a').on('click',function(){
    	  /*
		var index = $(this).index();
		if(index == 0 && $.trim(weibo) == ""){
			return ;
		}else if(index == 1 && $.trim(phone) == ""){
			return ;
		}else if(index == 2 && $.trim(email) == ""){
			return ;
		}
		if(index == 2 && !isBindEmail){
			$(".j_popup_box span").text("此账号还未绑定邮箱，无法发送邮件");
        	$(".j_popup_box").show();
			return ;
		}
		*/
		$(this).toggleClass('on');  
      })
      $('.btns_send').on('click',function(){
    	  sendType();
        if($.trim($('.import').val())==""){
        	$(".j_popup_box span").text("添加发送内容");
        	$(".j_popup_box").show();
        }else if(sendTypeStr == ""){
        	$(".j_popup_box span").text("请勾选发送方式");
        	$(".j_popup_box").show();
        }else{
            var Message = "";
            msg = $('.import').val().split("\n");
            sendType();
            for(var i = 0;i<msg.length;i++){
                if(msg[i]!=""){
                    Message+='<div class="service"><span class="time">'+new Date().format("yyyy-MM-dd hh:mm")+'</span><div class="busin"><i><img src="<%=path%>/images/demo_7.png"></i><div class="hails"><b><img src="<%=path%>/images/san.png"></b><em>'+userName+'</em><abbr>'+sendTypeStr+'：'+msg[i]+'</abbr></div></div></div>';
                }
            }
            
        	$.post(path+"/scan/findPeople/saveChat",{sendUserName:userName,wbUserId:wbUserId,statusId:tempStatusId,
        		content:$('.import').val(),type:sendTypeStr
        	},function(data){
      			if(data.msg_code == 1){
      				$('.m_xtalk').append(Message);
      				customerChatChange = 1;
			       	//定位滚动轴
			      	$(".m_xtalk").animate({scrollTop:$('.m_xtalk')[0].scrollHeight},2000);
      			}else{
      				alert("回复失败");
//       				$(".j_popup_box").show();
        		}
      		},"json");
            
	        msg = [];
            $('.import').val("");
            $('.J_xkelvin .cash').removeClass('on');

            //$('.m_xtalk').scrollTop( $('.m_xtalk')[0].scrollHeight );
            
        }
      })
    });
    
    $('.s_title li').click(function(){
        $(this).addClass('s_on').siblings().removeClass();
        var index = $(this).index();
        $('.s_container ol').removeClass('surrent').eq(index).addClass('surrent');
    });

    $('.s_all_title li').click(function(){
        $(this).addClass('s_all_on').siblings().removeClass();
        var index = $(this).index();
        $('.s_table table').removeClass('s_table_sur').eq(index).addClass('s_table_sur');
    });

    $('.m_xmanage li').click(function(){
        $(this).addClass('on').siblings().removeClass();
        var index = $(this).index();
        $('.s_kh_tab > div').removeClass('k_on').eq(index).addClass('k_on');
    });
</script>
<script type="text/javascript">
var path = '<%=path%>';
var isWeibo = 0;
var isDuanXin = 0;
var isEmail = 0;
var sendTypeStr = "";
var sendTypeInt = "";

var email = '';	//这些是客户的信息
var phone = '';

var isBindEmail = false;	//这个是当前登录用户是否绑定
var isBindWeibo = false;	//是否绑定微博，没有绑定，或者已过期，返回false


var wbUserId = getUrlParam("wbUserId");
var statusId = getUrlParam("statusId");
var tempStatusId = "";

$(function(){
	init();
	
	$(document).on("click","#customerEdit",function(){
		findByCustomerId(customerId);
	});
	
	
});

function init(){
	func_bExpired();
	func_getType();
	var param = {};
	if(wbUserId != null){
		param.wbUserId = wbUserId;
	}
	if(statusId != null){
		param.statusId = statusId;
	}
	$.post(path+'/scan/findPeople/getChat',param,function(data){
		data = data.data;
		func_weibo(data.weibo);
		if(data.customerInfo != null){
			customerInfo(data.customerInfo);
			weiDu(data.weiDu);
			customerCost(data.customerCost);
		}else{
			$("#spanWeibo").text(data.weibo.user_screen_name);
		}
		customerChat(data.customerChat);
// 		chatRemark(data.chatRemark);
		activity(data.activity);
	},"json");
	
	$.post('<%=path%>/api/weibo/SelectEmailbind',function(data){
		if(data.error_code == "0"){
			isBindEmail = true;
		}else{
			isBindEmail = false;
		}
	},"json");
	
<%-- 	$.post('<%=path%>/api/weibo/showfirstpage',function(data){ --%>
// 		console.log(data);
// 		if(data.showstatus == null){
// 			isBindWeibo = false;
// 		}
// 	},"json");
}

function func_bExpired(){
	$.ajax({
		async: false,
		dataType:"json",
		url:path+"/scan/findPeople/bExpired",
		success: function(data){
			if(data.msg_code == 1){
				if(data.data.code == 1){
					isBindWeibo = true;
				}else if(data.data.code == 2){
					isBindWeibo = false
				}else if(data.data.code == 3){
					isBindWeibo = false
				}
			}
		}
	});
}

function reloadCustomerInfo(){
	var param = {};
	if(wbUserId != null){
		param.wbUserId = wbUserId;
	}
	if(statusId != null){
		param.statusId = statusId;
	}
	$.post(path+'/scan/findPeople/getChat',param,function(data){
		customerInfo(data.customerInfo);
	},"json");
}

function customerInfo(customerInfo){
	
	if(customerInfo!=null){
		$(".name>span").text(customerInfo.name);
		$(".name img").attr("src",customerInfo.avatar);
		$(".contact #spanQq").text(customerInfo.qq);
		$(".contact #spanWeibo").text(customerInfo.weibo);
		$(".contact #spanWeixin").text(customerInfo.wechat);
		$(".contact #spanPhone").text(customerInfo.phone);
		$(".contact #spanEmail").text(customerInfo.email);
		email = customerInfo.email;
		phone = customerInfo.phone;
		
		if(tempStatusId == ""){
			$(".J_xselect>a:eq(0)").hide();
		}else{
			$(".J_xselect>a:eq(0)").show();
		}
		if($.trim(phone) == ""){
			$(".J_xselect>a:eq(1)").hide();
		}else{
			$(".J_xselect>a:eq(1)").show();
		}
		if($.trim(email) == ""){
			$(".J_xselect>a:eq(2)").hide();
		}else{
			$(".J_xselect>a:eq(2)").show();
		}
	}
}

function weiDu(weiDu){
	$(".tans").html("");
	if(weiDu != null && weiDu != undefined){
		if(weiDu.newOrOldCustomer == 2){
			$(".tans").append('<img src="<%=path%>/images/host_6.png" />');
		}else if(weiDu.newOrOldCustomer == 3){
			$(".tans").append('<img src="<%=path%>/images/host_3.png" />');
		}
		if(weiDu.transactionFrequency == 2){
			$(".tans").append('<img src="<%=path%>/images/host_2.png" />');
		}else if(weiDu.transactionFrequency == 3){
			$(".tans").append('<img src="<%=path%>/images/host_7.png" />');
		}else if(weiDu.transactionFrequency == 4){
			$(".tans").append('<img src="<%=path%>/images/host_5.png" />');
		}
		if(weiDu.transactionLimit == 2){
			$(".tans").append('<img src="<%=path%>/images/host_1.png" />');
		}else if(weiDu.transactionLimit == 3){
			$(".tans").append('<img src="<%=path%>/images/host_8.png" />');
		}else if(weiDu.transactionLimit == 4){
			$(".tans").append('<img src="<%=path%>/images/host_9.png" />');
		}
	}
}

function customerCost(customerCost){
	if(customerCost != null){
		$(".contact #spanTotalCost").text(customerCost.totalCost==null?0:customerCost.totalCost);
		$(".contact #emMaxCost").text(customerCost.maxCost);
		$(".contact #spanMaxDate").text(customerCost.maxDate);
		$(".contact #emMinCost").text(customerCost.minCost);
		$(".contact #spanMinDate").text(customerCost.minDate);
	}else{
		$(".contact #spanTotalCost").text("无");
		$(".contact #emMaxCost").text("无");
		$(".contact #spanMaxDate").text("无");
		$(".contact #emMinCost").text("无");
		$(".contact #spanMinDate").text("无");
	}
}

/**
 * 	客户行为信息，这玩意儿同样用于微博；
 */
function func_weibo(weibo){
	tempStatusId = weibo.status_id;
	$(".conve>.time").text(weibo.statusCreatedAt);
	$(".conve>.main img").attr("src",weibo.user_profile_image_url);
	$(".conve>.main>p>strong>b").text(weibo.user_screen_name);
	$(".conve>.main>p>em").text(weibo.status_text);
	
	$(".conve.main.p #status_reposts_count").text("转发："+weibo.status_reposts_count);
	$(".conve.main.p #status_comments_count").text("评论："+weibo.status_comments_count);
	$(".conve.main.p #status_attitudes_count").text("赞同："+weibo.status_attitudes_count);
}

function customerChat(customerChat){
	$(".service").html("");
	for (var i = 0; i < customerChat.length; i++) {
		var a = customerChat[i];
		
		var str = '<span class="time">'+a.createTime+'</span><div class="busin"><i><img src="'+path+'/images/demo_7.png" /></i><div class="hails"><b><img src="'+path+'/images/san.png" /></b><em>来自'+a.send_user_name+'</em><abbr>'+a.type+'：'+a.content+'</abbr></div></div>';
		
		$(".service").append(str);
	}
}

function chatRemark(chatRemark){
	$(".businTop").remove();
	for (var i = 0; i < chatRemark.length; i++) {
		var a = chatRemark[i];
		
		var str = '<div class="businTop"><span class="time timeBox">'+a.createTime+'</span><div class="busin businBox"><div class="hails"><dfn>'+a.content+'</dfn></div><div class="edit"><img src="<%=path%>/images/xicon_4.png" class="k_close_icon" chatRemarkId="'+a.id+'"/></div></div></div>';
		
		$(".m_xtalk").append(str);
	}
	
	//定位滚动轴
	$(".m_xtalk").animate({scrollTop:$('.m_xtalk')[0].scrollHeight},2000);
}

function activity(activity){
	$(".J_xkelvin").html("");
	for (var i = 0; i < activity.length; i++) {
		var a = activity[i];
		
		$(".J_xkelvin").append('<div class="cash" short_url="'+a.short_url+'" web_url="'+a.web_url+'"><span>'+a.name+'</span></div>');
	}
}

function sendType(){
	sendTypeStr = "";
	if($(".J_xselect>a:eq(0)").attr("class") == "on"){
		isWeibo = 1;
		sendTypeStr += ",微博";
		sendTypeInt += ",1";
	}
	if($(".J_xselect>a:eq(1)").attr("class") == "on"){
		isDuanXin = 1;
		sendTypeStr += ",短信";
		sendTypeInt += ",2";
	}
	if($(".J_xselect>a:eq(2)").attr("class") == "on"){
		isEmail = 1;
		sendTypeStr += ",邮件";
		sendTypeInt += ",3";
	}
	if(sendTypeStr != ""){
		sendTypeStr = sendTypeStr.substring(1);
		sendTypeInt = sendTypeInt.substring(1);
	}
}

function func_getType(){
	if(!isBindWeibo){
		$(".J_xselect>a:eq(0)").hide();
	}else{
		$(".J_xselect>a:eq(0)").show();
	}
	if($.trim(phone) == ""){
		$(".J_xselect>a:eq(1)").hide();
	}else{
		$(".J_xselect>a:eq(1)").show();
	}
	if($.trim(email) == "" || !isBindEmail){
		$(".J_xselect>a:eq(2)").hide();
	}else{
		$(".J_xselect>a:eq(2)").show();
	}
}

Date.prototype.format = function(fmt) { 
    var o = { 
       "M+" : this.getMonth()+1,                 //月份 
       "d+" : this.getDate(),                    //日 
       "h+" : this.getHours(),                   //小时 
       "m+" : this.getMinutes(),                 //分 
       "s+" : this.getSeconds(),                 //秒 
       "q+" : Math.floor((this.getMonth()+3)/3), //季度
       "S"  : this.getMilliseconds()             //毫秒 
   }; 
   if(/(y+)/.test(fmt)) {
           fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
   }
    for(var k in o) {
       if(new RegExp("("+ k +")").test(fmt)){
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        }
    }
   return fmt; 
}

function findByCustomerId(id){
	$.post(path+'/api/custome/findByCustomerId', {
		"customerID" : id,
	}, function(data) {
		data = JSON.parse(data);
		if(data.bcCustomer != 0){
			resetCustomer();
			$("#namea").val(isnull(data.bcCustomer.name));
			$("#phonea").val(isnull(data.bcCustomer.phone));
			$("#emaila").val(isnull(data.bcCustomer.email));
			$("#companya").val(isnull(data.bcCustomer.company));
			$("#joba").val(isnull(data.bcCustomer.job));
			$("#remarksa").val(isnull(data.bcCustomer.remarks));
			$("#gendera").val(isnull(data.bcCustomer.gender));
			$("#birthdaya").val(isnull(data.bcCustomer.birthday));
			$("#weiboa").val(isnull(data.bcCustomer.weibo));
			$("#qqa").val(isnull(data.bcCustomer.bak1));
			$("#wechata").val(isnull(data.bcCustomer.wechat));
			$("#addressa").val(isnull(data.bcCustomer.address));
			$("#citya").val(isnull(data.bcCustomer.city));
			$("#faxa").val(isnull(data.bcCustomer.fax));
			$("#taga").val(isnull(data.bcCustomer.tag));
			$("#customerID").val(id);
		}else{
			$(".j_popup_box span").text("编辑查询失败,未获取客户标识！");
        	$(".j_popup_box").show();
		}
	});
	/*zhiwen  */
		$('.m_xconsum').css('margin-top','-120px');
	$('.j_customerEdit').addClass('show');
}

function saveOrUpdate(){
	var cID = customerId;
	var myregPhone  = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/; 
	var myregEmail = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
	if($("#phonea").val() == '' || $("#phonea").val() == null){
		$(".j_popup_box span").text("手机号码不能为空！");
    	$(".j_popup_box").show();
	    return false; 
	}else{
		if(!myregPhone.test($("#phonea").val()))  { 
		    $(".j_popup_box span").text("请输入有效的手机号码！");
	    	$(".j_popup_box").show();
		    return false; 
		}
	}
	if( $("#emaila").val() == '' || $("#emaila").val() == null){
		
	}else{
		if(!myregEmail.test($("#emaila").val()))  { 
		    $(".j_popup_box span").text("请输入正确的邮箱！");
	    	$(".j_popup_box").show();
		    return false; 
		}
	}
	if(cID != ''){
		editCustomer(cID);
	}
}

function getSourceTitle(source){
	var title;
	switch (source) {
	case "1":
		title="微博";
		break;
	case "2":
		title="微信";
		break;
	case "3":
		title="QQ";
		break;
	case "4":
		title="活动";
		break;
	case "5":
		title="pc网页";
		break;
	default:
		title="未知";
		break;
	}
	return title;
	
}
//修改客户资料
function editCustomer(cID){
	$.post(path+'/api/custome/editCustomer', {
		"customerID" : cID,
		"name" : $("#namea").val(),
		"phone" : $("#phonea").val(),
		"email" : $("#emaila").val(),
		"company" : $("#companya").val(),
		"job" : $("#joba").val(),
		"remarks" : $("#remarksa").val(),
		"gender" : $("#gendera").val(),
		"birthday" : $("#birthdaya").val(),
		"weibo" : $("#weiboa").val(),
		"qq" : $("#qqa").val(),
		"wechat" : $("#wechata").val(),
		"city" : $("#citya").val(),
		"fax" : $("#faxa").val(),
		"tag" : $("#taga").val(),
		"address" : $("#addressa").val(),
	}, function(data) {
		$('.j_customerEdit').removeClass('show');
		reloadCustomerInfo();
		$(".j_popup_box span").text(data.message);
		/*zhiwen  */
		$('.m_xrecord').css('height','568px');
    
    	$(".j_popup_box").show();
	});
}

//判断是否为空
function isnull(str){
	if(str == null ){
		return '';
	}else{
		return str;
	}
}

function resetCustomer(){
	$("#namea").val('');
	$("#phonea").val('');
	$("#emaila").val('');
	$("#companya").val('');
	$("#joba").val('');
	$("#remarksa").val('');
	$("#gendera").val('');
	$("#birthdaya").val('');
	$("#weiboa").val('');
	$("#qqa").val('');
	$("#wechata").val('');
	$("#addressa").val('');
	$("#citya").val('');
	$("#faxa").val('');
	$("#taga").val('');
	$("#customerID").val('');
}

function getUrlParam(key){
	var reg = new RegExp("(^|&)"+ key +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}
</script>
</html>